package com.tech.connect.zhaorencai;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.GridOffsetsItemDecoration;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.model.ItemCatSub;
import com.tech.connect.util.CatUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class RenCaiOptionsChooseActivity extends BaseActivity {

    private RecyclerView recycler1;
    private RecyclerView recycler2;
    private RecyclerView recycler3;
    private RecyclerView recycler4;
    private List<ItemCatSub> all1 = new ArrayList<>();
    private List<ItemCatSub> all2 = new ArrayList<>();
    private List<ItemCatSub> all3 = new ArrayList<>();
    private List<ItemCatSub> all5 = new ArrayList<>();
    private BaseAdapter<ItemCatSub, BaseHolder> adapter1;
    private BaseAdapter<ItemCatSub, BaseHolder> adapter2;
    private BaseAdapter<ItemCatSub, BaseHolder> adapter3;
    private BaseAdapter<ItemCatSub, BaseHolder> adapter4;
//    private TextView tvAll;
//    private TextView tvOnLine;

//    private ItemCatSub default1 = new ItemCatSub("-1", "不限");
//    private ItemCatSub default2 = new ItemCatSub("all", "所有");
//    private ItemCatSub default3 = new ItemCatSub("0", "不限");
//    private ItemCatSub default4 = new ItemCatSub("0", "不限");
//    private ItemCatSub default5 = new ItemCatSub("0", "不限");

    private ItemCatSub select1;
    private ItemCatSub select2;
    private ItemCatSub select3;
    private ItemCatSub select5;
    private LinearLayout container;
    private List<TextView> textViews = new ArrayList<>();
    private ItemCatSub salary;
    private ItemCatSub education;
    private ItemCatSub work;
    private ItemCatSub userStatus;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("筛选条件");
        setContentView(R.layout.activity_option_choose);
        initView();
        Intent intent = getIntent();
        Serializable salarySer = intent.getSerializableExtra("salary");
        Serializable educationSer = intent.getSerializableExtra("education");
        Serializable workSer = intent.getSerializableExtra("work");
        Serializable userStatusSer = intent.getSerializableExtra("userStatus");
        if (salarySer != null && salarySer instanceof ItemCatSub) {
            salary = (ItemCatSub) salarySer;
        }
        if (educationSer != null && educationSer instanceof ItemCatSub) {
            education = (ItemCatSub) educationSer;
        }
        if (workSer != null && workSer instanceof ItemCatSub) {
            work = (ItemCatSub) workSer;
        }
        if (userStatusSer != null && userStatusSer instanceof ItemCatSub) {
            userStatus = (ItemCatSub) userStatusSer;
        }
        loadData();
    }

    private void loadData() {
        List<ItemCatSub> salary = CatUtils.getInstance().getCatListByCode("salary");
        List<ItemCatSub> education = CatUtils.getInstance().getCatListByCode("education");
        List<ItemCatSub> work = CatUtils.getInstance().getCatListByCode("work");
        List<ItemCatSub> userStatus = CatUtils.getInstance().getCatListByCode("user-status");
        initAdapter1(salary);
        initAdapter2(education);
        initAdapter3(work);
        initAdapter5(userStatus);
    }

    private void initAdapter5(List<ItemCatSub> list) {
        container.removeAllViews();
        textViews.clear();
        all5.clear();
        if (list != null) {
            all5.addAll(list);
        }
        if (userStatus == null && !all5.isEmpty()) {
            userStatus = all5.get(0);
        }
        select5 = userStatus;
        for (int i = 0; i < all5.size(); i++) {
            ItemCatSub optChoose = all5.get(i);
            TextView textView = generateTextView();
            textView.setText(optChoose.description);
            textView.setTag(optChoose);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object tag = v.getTag();
                    if (tag != null && tag instanceof ItemCatSub) {
                        select5 = (ItemCatSub) tag;
                        notify5();
                    }
                }
            });
            textViews.add(textView);
            container.addView(textView);
        }
        notify5();
    }

    private void notify5() {
        for (TextView textView : textViews) {
            Object tag = textView.getTag();
            if (tag != null && tag instanceof ItemCatSub) {
                ItemCatSub temp = (ItemCatSub) tag;
                if (temp.equals(select5)) {
                    textView.setSelected(true);
                } else {
                    textView.setSelected(false);
                }
            } else {
                textView.setSelected(false);
            }
        }
    }

    private TextView generateTextView() {
        TextView textView = new TextView(activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AndroidUtil.dip2px(80), AndroidUtil.dip2px(35));
        params.rightMargin = AndroidUtil.dip2px(10);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        textView.setGravity(Gravity.CENTER);
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setLayoutParams(params);
        textView.setBackgroundResource(R.drawable.common_select_corner5_yellow_with_gray);
        textView.setTextColor(activity.getResources().getColorStateList(R.color.common_select_white_with_dark));
        return textView;
    }

    private void initAdapter1(List<ItemCatSub> list) {
        all1.clear();
        if (list != null) {
            all1.addAll(list);
        }
        if (salary == null && !all1.isEmpty()) {
            salary = all1.get(0);
        }
        select1 = salary;
        if (adapter1 == null) {
            adapter1 = new BaseAdapter<ItemCatSub, BaseHolder>(R.layout.item_layout_text, all1) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemCatSub s = all1.get(position);
                    TextView view = helper.getView(R.id.tv);
                    view.setText(s.description);
                    if (s.equals(select1)) {
                        view.setSelected(true);
                        view.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                    } else {
                        view.setSelected(false);
                        view.setBackgroundResource(R.drawable.common_shape_corner5_stroke_gray);
                    }
                }
            };
            adapter1.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    select1 = all1.get(position);
                    adapter1.notifyDataSetChanged();
                }
            });
            recycler1.setAdapter(adapter1);
        }
        adapter1.notifyDataSetChanged();
    }

    private void initAdapter2(List<ItemCatSub> list) {
        all2.clear();
        if (list != null) {
            all2.addAll(list);
        }
        if (education == null && !all2.isEmpty()) {
            education = all2.get(0);
        }
        select2 = education;
        if (adapter2 == null) {
            adapter2 = new BaseAdapter<ItemCatSub, BaseHolder>(R.layout.item_layout_text, all2) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemCatSub s = all2.get(position);
                    TextView view = helper.getView(R.id.tv);
                    view.setText(s.description);
                    if (s.equals(select2)) {
                        view.setSelected(true);
                        view.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                    } else {
                        view.setSelected(false);
                        view.setBackgroundResource(R.drawable.common_shape_corner5_stroke_gray);
                    }
                }
            };
            adapter2.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    select2 = all2.get(position);
                    adapter2.notifyDataSetChanged();
                }
            });
            recycler2.setAdapter(adapter2);
        }
        adapter2.notifyDataSetChanged();
    }

    private void initAdapter3(List<ItemCatSub> list) {
        all3.clear();
        if (list != null) {
            all3.addAll(list);
        }
        if (work == null && !all3.isEmpty()) {
            work = all3.get(0);
        }
        select3 = work;
        if (adapter3 == null) {
            adapter3 = new BaseAdapter<ItemCatSub, BaseHolder>(R.layout.item_layout_text, all3) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemCatSub s = all3.get(position);
                    TextView view = helper.getView(R.id.tv);
                    view.setText(s.description);
                    if (s.equals(select3)) {
                        view.setSelected(true);
                        view.setBackgroundResource(R.drawable.common_shape_corner5_stroke_yellow);
                    } else {
                        view.setSelected(false);
                        view.setBackgroundResource(R.drawable.common_shape_corner5_stroke_gray);
                    }
                }
            };
            adapter3.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    select3 = all3.get(position);
                    adapter3.notifyDataSetChanged();
                }
            });
            recycler3.setAdapter(adapter3);
        }
        adapter3.notifyDataSetChanged();
    }


    private void initView() {
        container = findViewById(R.id.container);
        TextView tvTag1 = findViewById(R.id.tvTag1);
        TextView tvTag2 = findViewById(R.id.tvTag2);
        TextView tvTag3 = findViewById(R.id.tvTag3);
        TextView tvTag4 = findViewById(R.id.tvTag4);
        View line4 = findViewById(R.id.line4);
        tvTag1.setText("薪资");
        tvTag2.setText("学历");
        tvTag3.setText("工作经验");
        tvTag4.setVisibility(View.GONE);
        line4.setVisibility(View.GONE);
        recycler1 = findViewById(R.id.recycler1);
        recycler2 = findViewById(R.id.recycler2);
        recycler3 = findViewById(R.id.recycler3);
        recycler4 = findViewById(R.id.recycler4);
        recycler4.setVisibility(View.GONE);
//        tvAll = findViewById(R.id.tvAll);
//        tvAll.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                tvAll.setSelected(true);
//                tvOnLine.setSelected(false);
//            }
//        });
//        tvAll.setSelected(false);
//        tvOnLine = findViewById(R.id.tvOnLine);
//        tvOnLine.setSelected(true);
//        tvOnLine.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                tvAll.setSelected(false);
//                tvOnLine.setSelected(true);
//            }
//        });
        findViewById(R.id.btReset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (all1 != null) {
                    select1 = all1.get(0);
                }
                if (all2 != null) {
                    select2 = all2.get(0);
                }
                if (all3 != null) {
                    select3 = all3.get(0);
                }

                if (all5 != null) {
                    select5 = all5.get(0);
                }
                adapter1.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
                adapter3.notifyDataSetChanged();
                adapter4.notifyDataSetChanged();
//                tvAll.performClick();
                notify5();
            }
        });
        findViewById(R.id.btEnter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("salary", select1);
                intent.putExtra("education", select2);
                intent.putExtra("work", select3);
                intent.putExtra("userStatus", select5);
                setResult(RESULT_OK, intent);
                destroyActivity();
            }
        });
        GridOffsetsItemDecoration offsetsItemDecoration = new GridOffsetsItemDecoration(
                GridOffsetsItemDecoration.GRID_OFFSETS_VERTICAL);
        offsetsItemDecoration.setVerticalItemOffsets(AndroidUtil.dip2px(8));
        offsetsItemDecoration.setHorizontalItemOffsets(AndroidUtil.dip2px(8));
        recycler1.addItemDecoration(offsetsItemDecoration);
        recycler2.addItemDecoration(offsetsItemDecoration);
        recycler3.addItemDecoration(offsetsItemDecoration);
        recycler1.setLayoutManager(new GridLayoutManager(activity, 3));
        recycler2.setLayoutManager(new GridLayoutManager(activity, 3));
        recycler3.setLayoutManager(new GridLayoutManager(activity, 3));
    }
}
